package com.eningqu.aipen.ui.base;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Package:com.eningqu.aipen.ui.base
 * Author:starr
 * Time:2023/11/7 12:30
 * Description: 基类Fragment，基于反射实现
 */
public abstract class BaseFragment<VB extends ViewDataBinding> extends Fragment {

    public VB mBinding;

    private boolean isFirstLoad = true;//是否第一次可见

    private final String TAG = "BaseFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        Log.d(TAG, "onCreateView: " + getClass().getSimpleName());
        Type superClass = getClass().getGenericSuperclass();
        assert superClass != null;
        Class<?> aClass = (Class<?>) ((ParameterizedType) superClass).getActualTypeArguments()[0];
        try {
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
            mBinding = (VB) method.invoke(null, getLayoutInflater(), container, false);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        initStatusBar();
        return mBinding.getRoot();
    }


    //用户可见时执行此方法
    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: " + getClass().getSimpleName());

        if (isFirstLoad) {
            async();
            isFirstLoad = false;
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.d(TAG, "onAttach: " + getClass().getSimpleName());


    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: " + getClass().getSimpleName());

    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: " + getClass().getSimpleName());


    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: " + getClass().getSimpleName());

    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: " + getClass().getSimpleName());

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: " + getClass().getSimpleName());

    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(TAG, "onDetach: " + getClass().getSimpleName());

    }


    /**
     * 用于子类实现
     */
    protected abstract void async();

    /**
     * 状态栏
     */
    protected abstract void initStatusBar();
}
